module btoolkit {
	export class BTimer {

		private onTimerCallback: Function;
		private onFinishCallback: Function;
		private timer: egret.Timer;
		private thisOjbect: any;

		constructor(intervalMS: number, times: number, onTimerCallback: Function, onFinishCallback: Function, thisOjbect: any) {
			this.onTimerCallback = onTimerCallback;
			this.onFinishCallback = onFinishCallback;
			this.thisOjbect = thisOjbect;
			this.timer = new egret.Timer(intervalMS, times);
			this.timer.addEventListener(egret.TimerEvent.TIMER, () => {
				if (this.thisOjbect && this.onTimerCallback) {
					egret.callLater(this.onTimerCallback, this.thisOjbect);
				}
			}, this);
			this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, () => {
				if (this.thisOjbect && this.onFinishCallback) {
					egret.callLater(this.onFinishCallback, this.thisOjbect);
				}
			}, this);
		}

		start(intervalMS?: number, times?: number) {
			if (intervalMS) {
				this.timer.delay = intervalMS;
			}
			if (times) {
				this.timer.repeatCount = times;
			}
			this.timer.reset();
			this.timer.start();
		}

		stop(): void {
			this.timer.stop();
		}

	}
}